package org.example;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.jupiter.api.Test;

import java.util.Calendar;
import java.util.HashMap;

class StpringBootTest1 {

    @Test
    void contextLoads() {
        HashMap<String, Object> map = new HashMap<>();
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MINUTE, 20);

        String token = JWT.create()
                .withHeader(map)
                .withClaim("userId", 21)
                .withClaim("username", "张三")
                .withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256("!RANSHIYAN"));

        System.out.println(token);
    }

    @Test
    public void test(){

        JWTVerifier build = JWT.require(Algorithm.HMAC256("!RANSHIYAN")).build();

        DecodedJWT verify = build.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTY5MTI5MTMsInVzZXJJZCI6MjEsInVzZXJuYW1lIjoi5byg5LiJIn0.dyMPogDyAzJLcSs09qBCk_SeHZNTW5l7Vea2isz5QIo");

        System.out.println(verify.getClaim("userId").asInt()+","+verify.getClaim("username").asString());
    }
}